//
//  MODoctorListController.m
//  FastDoctor
//
//  Created by 冯龙飞 on 16/5/22.
//  Copyright © 2016年 冯龙飞. All rights reserved.
//

#import "MODoctorListController.h"
#import "LSHTTPManager.h"
#import "MODoctorListCell.h"
#import "MOConcernModel.h"
#import <MJRefresh/MJRefresh.h>
#import "MODocMessageController.h"

@interface MODoctorListController ()

@property(nonatomic, strong) MJRefreshNormalHeader * header;
@property(nonatomic, strong) MJRefreshFooter * footer;

@end

// cell的标识
static NSString *MODoctorListCellID = @"MODoctorListCell";

@implementation MODoctorListController

- (void)viewDidLoad {
    [super viewDidLoad];
    //重写返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_button_back_01"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    // 在这个方法中请求数据并给数组赋值
    [self loadData];
    
    // 注册cell
    [self.tableView registerClass:[MODoctorListCell class] forCellReuseIdentifier:MODoctorListCellID];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self downPush];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block,这里暂时模拟延时刷新
        [self upPush];
    }];
}

//重写返回按钮,判断是否打开菜单侧滑
- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
    [NQNavVc judgeMenuOpen];
}

// 这个方法利用block的回调给数组赋值
- (void)loadData {
    [[LSHTTPManager sharedManager] concernDoctor:^(NSArray *arr1) {
        
        self.model = arr1;
        
        [self.tableView reloadData];
        
    }];
    
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.model.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MODoctorListCell *cell = [tableView dequeueReusableCellWithIdentifier:MODoctorListCellID forIndexPath:indexPath];
    
    MOConcernModel *model = self.model[indexPath.row];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.model = model;
    
    return cell;
}

// 返回cell的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 90;
}

// 点击cell后调用的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 跳转控制器,并把模型传过去
    MODocMessageController *doc = [[MODocMessageController alloc]init];
    doc.model = self.model[indexPath.row];
    
    doc.title = @"医生基本信息";
    
    [self.navigationController pushViewController:doc animated:YES];
}

// 这里是模拟加载数据
- (void)downPush {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.tableView.header endRefreshing];
    });
    
}

- (void)upPush {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.tableView.mj_footer endRefreshing];
    });
}

- (NSArray *)model {
    
    if (_model == nil) {
        _model = [NSArray array];
    }
    return _model;
}

@end
